import os
import sys
import threading
import time

from lib.data import poclock
from lib.data import pocs


sys.path.append("lib");
sys.path.append("module");

def register(filename, pocClass):
    if filename not in pocs.keys():
        pocs[filename] = pocClass();


def loadpoc():
    for root, dirs, files in os.walk("module"):
        for file in files:
            if file.endswith(".py"):
                __import__(file.split(".")[0])


def loadpocBydir(dir):
    for root, dirs, files in os.walk(dir):
        for file in files:
            if file.endswith(".py"):
                __import__(file.split(".")[0])



def loadspecpoc(filename):
    if filename.endswith(".py"):
        __import__(filename.split("/")[-1].split(".")[0]);


class autoLoadPocThread(threading.Thread):
    def __init__(self):
        self._stop = threading.Event();
        super(autoLoadPocThread, self).__init__();

    def run(self):
        while True:
            if self._stop.isSet():
                break;
            time.sleep(60 * 15);
            poclock.acquire();
            loadpoc();
            poclock.release();
    
    def stop(self):
        self._stop.set();